package view.table
{
	import core.Control;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import global.GameState;
	import global.Resource;
	
	import utils.DisplayTool;
	import utils.TextTool;
	import utils.animate.AlphaMoveTween;
	import utils.animate.TweenLight;
	
	import view.card.ICard;
	import view.components.HintImgButton;
	import view.components.ImgButton;
	import view.components.ModulLoader;
	import view.components.RemoteImage;

	public class Table extends Sprite
	{
		private var _control:Control = Control.getInstance();
		
		private var bg:DisplayObject;
		private var showCardLayer:Sprite;
		private var sitButLayer:Sprite;
		private var bankerImg:DisplayObject;
		
		private var effectLayer:Sprite;
		private var sitLayer:Sprite;
		private var flyGoldLayer:Sprite;
		public var emoteLayer:Sprite;
		private var talkLayer:Sprite;
		
		private var tweenLight:TweenLight;
		private var maintainCardsText:TextField;
		private var multiText:TextField;
		
		private var giveCardTween:AlphaMoveTween;
		private var giveCardImg:DisplayObject;
		
		//
		private var mailBut:HintImgButton;
		private var soundBut:ImgButton;
		private var friendBut:ImgButton;
		private var feedBut:ImgButton;
		private var taskBut:ImgButton;
		private var itemBut:ImgButton;
		
		public function Table()
		{
			initBg();
			initTexts();
			
			initLayers();
			initImg();
			initTweenLight();
			initButs();
		}
		
		private function initButs():void
		{
			addChild(new ImgButton(720,0,0,onBut,48,23,"butBg","text_panel_hall"));
			addChild(new ImgButton(670,0,1,onBut,48,23,"butBg","text_panel_standup"));
			addChild(new ImgButton(620,0,2,onBut,48,23,"butBg","text_panel_hallHelp"));
			
			soundBut = new ImgButton(2,416,3,onBut,22,22,"butBg","face_soundOn");
			soundBut.info = "声音";
			soundBut.isUpInfo = true;
			addChild(soundBut);
			
			mailBut = new HintImgButton(132,416,4,onBut,Resource.getShape("butBg"),22,22,"face_mail");
			mailBut.info = "消息";
			mailBut.isUpInfo = true;
			addChild(mailBut);
			mailBut.visible = false;
			
			friendBut = new ImgButton(28,416,5,onBut,22,22,"butBg","face_friend");
			friendBut.info = "好友";
			friendBut.isUpInfo = true;
			addChild(friendBut);
			
			taskBut = new ImgButton(80,416,7,onBut,22,22,"butBg","face_task");
			taskBut.info = "任务";
			taskBut.isUpInfo = true;
			//taskBut.mouseEnabled = false;
			addChild(taskBut);
			
			feedBut = new ImgButton(54,416,6,onBut,22,22,"butBg","face_wrong");
			feedBut.info = "反馈意见";
			feedBut.isUpInfo = true;
			addChild(feedBut);
			
			itemBut = new ImgButton(106,416,8,onBut,22,22,"butBg","face_item");
			itemBut.info = "物品栏";
			itemBut.isUpInfo = true;
			//itemBut.mouseEnabled = false;
			addChild(itemBut);
			
		}
		
		public function showHideMailBut(v:Boolean):void
		{
			mailBut.visible = v;
			if (v)
			{
				mailBut.startHint();
			}
			else
			{
				mailBut.stopHint();
			}
		}
	
		private function onBut(event:MouseEvent):void
		{
			switch (event.target.id)
			{
				case 0://返回大厅
					_control.tableControl.backToHall();
					break;
				case 1:
					_control.tableControl.onStandUp();
					break;
				case 2://帮助
					_control.helpPanelControl.open(true);
					break;
				case 3:
					_control.soundControl.switchOnOff();
					if (GameState.isPlaySound)
					{
						event.target.resetFace("face_soundOn");
					}
					else
					{
						event.target.resetFace("face_soundOff");
					}
					
					break;
				case 4://mail
					_control.mailControl.mailButClick();
					break;
				case 5://好友
					_control.friendPanelControl.openOrClose();
					break;
				case 6://反馈
					_control.feedPanelControl.open();
					break;
				case 7:
					_control.taskPanelControl.open();
					break;
				case 8://物品栏
					_control.itemPanelControl.open(true);
					break;
			}
		}
		
		private function initTexts():void
		{
			maintainCardsText = TextTool.getTextField(80,19,12,0xFFCC00);
			multiText = TextTool.getTextField(80,19,12,0xFFFFFF);
			
			maintainCardsText.x = 380;
			maintainCardsText.y = 190;
			
			multiText.x = 380;
			multiText.y = 206;
			
			addChild(multiText);
			addChild(maintainCardsText);
			
			maintainCardsText.text = "剩余 0 张";
			multiText.text = "倍数 0";
		}
		
		private function initTweenLight():void
		{
			tweenLight = new TweenLight();
			giveCardTween = new AlphaMoveTween();
			
			giveCardTween.addEventListener(Event.COMPLETE,onGiveCardComplete);
		}
		
		private function initImg():void
		{
			bankerImg = Resource.getShape("banker");
			bankerImg.x = 320;
			bankerImg.y = 195;
			effectLayer.addChild(bankerImg);	
			
			giveCardImg = Resource.getImg("card_back");
			giveCardImg.scaleX = giveCardImg.scaleY = .5;
		}
		
		private function initLayers():void
		{
			sitButLayer = new Sprite();
			showCardLayer = new Sprite();
			effectLayer = new Sprite();
			sitLayer = new Sprite();
			flyGoldLayer = new Sprite();
			emoteLayer = new Sprite();
			talkLayer = new Sprite();
			
			addChild(sitButLayer);
			addChild(sitLayer);
			addChild(showCardLayer);
			addChild(effectLayer);
			addChild(flyGoldLayer);
			addChild(emoteLayer);
			addChild(talkLayer);
			
		}
		
	
		
		private function initBg():void
		{
			bg = new ModulLoader(780,550);
			(bg as ModulLoader).load("resource/roomBg.swf");
			(bg as ModulLoader).mouseChildren = false;
			(bg as ModulLoader).mouseEnabled = false;
			addChild(bg);
		}
	
		public function updateMultiRemain(remain:String,multi:String):void
		{
			maintainCardsText.text = "剩余 " + remain + " 张";
			multiText.text = "倍数 :" + multi;
		}
		
		public function updateBanker(sitId:int):void
		{
			tweenLight.duration = 1;
			tweenLight.setTarget(bankerImg,bankerImg.x,bankerImg.y,GameState.sitBankerXYS[sitId][0],GameState.sitBankerXYS[sitId][1]);
			tweenLight.start();
			
		}
		
		public function playGiveCardAnimate(sitId:int):void
		{
			var tox:int = GameState.giveCardXYS[sitId][0];
			var toy:int = GameState.giveCardXYS[sitId][1];
			giveCardTween.duration = .9;
			giveCardImg.alpha = 1;
			effectLayer.addChild(giveCardImg);	
			giveCardTween.setTarget(giveCardImg,365,205,tox,toy);
			giveCardTween.start();
		}
		
		public function onGiveCardComplete(event:Event):void
		{
			DisplayTool.removeChild(effectLayer,giveCardImg);
		}
		
		public function showCards(cards:Array):void
		{
			var card:ICard;
			for (var i:String in cards)
			{
				card = cards[i];
				showCardLayer.addChild(card.self);
			}
		}
		
		public function clearShowCards():void
		{
			DisplayTool.removeAllChild(showCardLayer);
		}
		
		public function addSitBut(but:DisplayObject):void
		{
			if (but)
			{
				sitButLayer.addChild(but);
			}
		}
		
		public function addSit(sit:DisplayObject):void
		{
			sitLayer.addChild(sit);
		}
		
		public function addGold(gold:DisplayObject):void
		{
			flyGoldLayer.addChild(gold);
		}
		
		public function addEmote(emote:DisplayObject):void
		{
			emoteLayer.addChild(emote);
		}
		
		public function addTalk(talk:DisplayObject):void
		{
			talkLayer.addChild(talk);
		}
		
		public function clear():void
		{
			tweenLight.stop();
			bankerImg.x = 320;
			bankerImg.y = 195;
			giveCardTween.stop();
			clearShowCards();
		}
	}
}